package nix.ssm.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtils {
	
    private static JedisPool jedisPool;
    private static ResourceBundle resource;
    
    static {
          InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
          try {
                resource = new PropertyResourceBundle(in);
          } catch (IOException e) {
                e.printStackTrace();
          }
          JedisPoolConfig poolConfig = new JedisPoolConfig();
          poolConfig = new JedisPoolConfig();
          poolConfig.setMaxTotal(Integer.parseInt(resource.getString("redis.maxTotal")));
          poolConfig.setMaxIdle(Integer.parseInt(resource.getString("redis.maxIdle")));
          poolConfig.setMinIdle(Integer.parseInt(resource.getString("redis.minIdle")));
          jedisPool = new JedisPool(poolConfig, resource.getString("redis.host"),
                      Integer.parseInt(resource.getString("redis.port")));
    }
    
    public static Jedis getJedis() {
          Jedis jedis = jedisPool.getResource();
          jedis.auth(resource.getString("redis.pass"));
          return jedis;
    }
}
